import {
    goods
} from "../../goods.js"

Page({

    /**
     * 页面的初始数据
     */

    data: {
        headerstyle: '',
        swiperList: [],
        active: {},
        show: false,
        list: goods,
        orders: {},

        sku: {}
    },
    //详情页面的样式切换
    add(e){
        this.setData({
            id:e.currentTarget.dataset["price"],
        })
    },
    // 外卖，自取按钮切换
    Click1() {
        this.setData({
            num: 1
        })
    },
    Click2() {
        this.setData({
            num: 2
        })

    },

    switchNav: function (e) {
        var page = this;
        var id = e.target.id;
        if (this.data.currentTab == id) {
            return false;
        } else {
            page.setData({
                currentTab: id
            });
        }
        page.setData({
            flag: id
        });
    },
    close() {
        this.setData({
            show: false,
            sku: {}
        })
    },
    select(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            active: this.data.list[index],
            show: true,
        })
    },
    addGood: function (e) {
        let carts = wx.getStorageSync('orders') || [];
        var id = e.currentTarget.id;
        let index = carts.findIndex(item => {
            return id == this.data.active.id;
        })

        if (index === -1) {
            this.data.orders.id = this.data.active.id;
            this.data.orders.name = this.data.active.name;
            this.data.orders.pic = this.data.active.pic;
            this.data.orders.price = this.data.active.price;
            this.data.orders.num = 1;
            this.data.orders.selected = true;
            var goods = wx.getStorageSync('goods');

            carts.push(this.data.orders);
            wx.setStorageSync('orders', carts);
        } else {
            carts[index].num++;
        }
        wx.setStorageSync('orders', carts);
        wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
        });
    },
    // click3:function(e){
    //     console.log(e);
    // },
})